package com.bilibili.server.controller;

import com.bilibili.server.dto.Response;
import com.bilibili.server.entity.Video;
import com.bilibili.server.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/videos")
public class VideoController {
    @Autowired
    private VideoService videoService;

    // 获取推荐视频列表
    @GetMapping("/recommended")
    public ResponseEntity<Response<Page<Video>>> getRecommendedVideos(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Video> videos = videoService.getRecommendedVideos(page, pageSize);
            return new ResponseEntity<>(Response.success("获取成功", videos), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 获取最新视频列表
    @GetMapping("/latest")
    public ResponseEntity<Response<Page<Video>>> getLatestVideos(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Video> videos = videoService.getLatestVideos(page, pageSize);
            return new ResponseEntity<>(Response.success("获取成功", videos), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 获取热门视频列表
    @GetMapping("/hot")
    public ResponseEntity<Response<Page<Video>>> getHotVideos(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Video> videos = videoService.getHotVideos(page, pageSize);
            return new ResponseEntity<>(Response.success("获取成功", videos), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 获取幻灯片视频列表
    @GetMapping("/slides")
    public ResponseEntity<Response<List<Video>>> getSlideVideos() {
        try {
            List<Video> videos = videoService.getSlideVideos();
            return new ResponseEntity<>(Response.success("获取成功", videos), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 根据类别获取视频列表
    @GetMapping("/category/{categoryId}")
    public ResponseEntity<Response<Page<Video>>> getVideosByCategory(
            @PathVariable int categoryId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            Page<Video> videos = videoService.getVideosByCategory(categoryId, page, pageSize);
            return new ResponseEntity<>(Response.success("获取成功", videos), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 获取视频详情
    @GetMapping("/{videoId}")
    public ResponseEntity<Response<Video>> getVideoDetail(@PathVariable Long videoId) {
        try {
            Video video = videoService.getVideoDetail(videoId);
            if (video == null) {
                return new ResponseEntity<>(Response.error(404, "视频不存在"), HttpStatus.NOT_FOUND);
            }
            return new ResponseEntity<>(Response.success("获取成功", video), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(Response.error(500, "服务器内部错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}